#include "temperature.h"

static FILE* fd;
static struct bcshm_t shm;

void temperature_start(){
/* 
  while(1){
    fd = fopen("/mnt/1wire/28.5CDA81E3A43C/temperature", "r");
    if(!fd)
      sleep(5);
    else 
      break;
  }
*/
  bcshm_open(&shm);
  
  struct ev_loop* loop = ev_loop_new(EVFLAG_NOENV | EVBACKEND_EPOLL);
  ev_timer main_timer;
  ev_init(&main_timer, main_timer_cb);
  main_timer.repeat = TEMP_TIMER;
  
  ev_timer_again(loop, &main_timer);
  ev_run(loop, 0); 
}

static void main_timer_cb(struct ev_loop *loop, ev_timer *w, int revents){
  FILE* fd = fopen("/mnt/1wire/28.5CDA81E3A43C/temperature", "r");
  if(!fd)
    return;
  char buf[64];
  size_t bl = fread(buf, sizeof(buf), 1, fd);

  bc_tlm_t bc_tlm;
  bc_tlm.temperature = atof(buf);
  bcshm_write(SHM_BC_TLM, &shm, &bc_tlm);
}
